//
//  LoginData.m
//  DA_V
//
//  Created by 周季阳 on 15/5/12.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "LoginData.h"
#import "AFHTTPRequestOperationManager.h"
#import "EncryptionData.h"
#import "JSONKit.h"
@implementation LoginData

#pragma mark - 登录回调
-(void)loginMobileNumber:(NSString *)MobileNumber andLoginPass:(NSString *)LoginPass andDeviceType:(NSString *)DeviceType andDeviceNumber:(NSString *)DeviceNumber andLastLoginIP:(NSString *)LastLoginIP{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    manager.requestSerializer.timeoutInterval = 20.0;
    // NSDictionary *paramer = @{@"MobileNumber":MobileNumber,@"LoginPass":LoginPass,@"DeviceType":DeviceType,@"DeviceNumber":DeviceNumber};
    
    
    
    //NSMutableArray *array = [[NSMutableArray alloc]init];
    NSMutableDictionary *  smallGoodsDic = [NSMutableDictionary dictionaryWithCapacity:1];
    
    
    [smallGoodsDic setObject:MobileNumber forKey:@"MobileNumber"];
    [smallGoodsDic setObject:LoginPass forKey:@"LoginPass"];
    [smallGoodsDic setObject:DeviceType forKey:@"DeviceType"];
    [smallGoodsDic setObject:DeviceNumber forKey:@"DeviceNumber"];
    [smallGoodsDic setObject:LastLoginIP forKey:@"LastLoginIP"];
    
    //[array addObject:smallGoodsDic];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:smallGoodsDic options:NSJSONWritingPrettyPrinted error:nil];
    
    
    NSString *jsonString = [jsonData base64EncodedStringWithOptions:0];
    
    NSString *str = [self encodeString:jsonString :@"hotkeycuk"];
    
    NSDictionary *paramer = @{@"sign":str};
    
    NSString *urls = [NSString stringWithFormat:@"http://api.dw521.com/api/login/login.php?action=login"];
    [manager POST:urls parameters:paramer success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"登录信息responseObject=%@",responseObject);
        EncryptionData *encryption = [[EncryptionData alloc]init];
        NSString * str =[encryption decryption:[responseObject objectForKey:@"sign"] :@"hotkeycuk"];
        NSDictionary *dic = [str objectFromJSONString];
        [self.delegate login:dic];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.delegate requistFail];
    }];
    
}

#pragma mark - 短信验证
-(void)sendMessageNumber:(NSString *)MobileNumber andServiceID:(NSString *)ServiceID{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    //NSMutableArray *array = [[NSMutableArray alloc]init];
    NSMutableDictionary *  smallGoodsDic = [NSMutableDictionary dictionaryWithCapacity:1];
    
    [smallGoodsDic setObject:MobileNumber forKey:@"MobileNumber"];
    [smallGoodsDic setObject:ServiceID forKey:@"ServiceID"];
    
    //[array addObject:smallGoodsDic];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:smallGoodsDic options:NSJSONWritingPrettyPrinted error:nil];
    
    NSString *jsonString = [jsonData base64EncodedStringWithOptions:0];
    
    // EncryptionData *data  = [[EncryptionData alloc]init];
    NSString *str = [self encodeString:jsonString :@"hotkeycuk"];
    
    NSDictionary *paramer = @{@"sign":str};
    
    NSString *urls = [NSString stringWithFormat:@"http://api.dw521.com/api/login/login.php?action=send"];
    [manager POST:urls parameters:paramer success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"登录信息responseObject=%@",responseObject);
        EncryptionData *encryption = [[EncryptionData alloc]init];
        NSString * str =[encryption decryption:[responseObject objectForKey:@"sign"] :@"hotkeycuk"];
        NSDictionary *dic = [str objectFromJSONString];
       
        [self.delegate sendMessage:dic];
 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error");
        [self.delegate requistFail];
    }];
}

#pragma mark -验证验证码
-(void)verifyMessageCode:(NSString *)CodeID andUserCode:(NSString *)UserCode andServiceID:(NSString *)ServiceID{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    //NSMutableArray *array = [[NSMutableArray alloc]init];
    NSMutableDictionary *  smallGoodsDic = [NSMutableDictionary dictionaryWithCapacity:1];
    [smallGoodsDic setObject:CodeID forKey:@"CodeID"];
    [smallGoodsDic setObject:UserCode forKey:@"UserCode"];
    [smallGoodsDic setObject:ServiceID forKey:@"ServiceID"];
    //[array addObject:smallGoodsDic];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:smallGoodsDic options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [jsonData base64EncodedStringWithOptions:0];
    // EncryptionData *data  = [[EncryptionData alloc]init];
    NSString *str = [self encodeString:jsonString :@"hotkeycuk"];
    NSDictionary *paramer = @{@"sign":str};
    NSString *urls = [NSString stringWithFormat:@"http://api.dw521.com/api/login/login.php?action=check"];
    [manager POST:urls parameters:paramer success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"登录信息responseObject=%@",responseObject);
        EncryptionData *encryption = [[EncryptionData alloc]init];
        NSString * str =[encryption decryption:[responseObject objectForKey:@"sign"] :@"hotkeycuk"];
        NSDictionary *dic = [str objectFromJSONString];
        [self.delegate verifyMessage:dic];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error");
        [self.delegate requistFail];
    }];
}

#pragma mark - 重置密码
-(void)updataPassword:(NSString *)AgentID andNewLoginPass:(NSString *)NewLoginPass andReNewLoginPass:(NSString *)ReNewLoginPass{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    //NSMutableArray *array = [[NSMutableArray alloc]init];
    NSMutableDictionary *  smallGoodsDic = [NSMutableDictionary dictionaryWithCapacity:1];
    
    [smallGoodsDic setObject:AgentID forKey:@"AgentID"];
    [smallGoodsDic setObject:NewLoginPass forKey:@"NewLoginPass"];
    [smallGoodsDic setObject:ReNewLoginPass forKey:@"ReNewLoginPass"];
    //[array addObject:smallGoodsDic];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:smallGoodsDic options:NSJSONWritingPrettyPrinted error:nil];
    
    NSString *jsonString = [jsonData base64EncodedStringWithOptions:0];
    
    // EncryptionData *data  = [[EncryptionData alloc]init];
    NSString *str = [self encodeString:jsonString :@"hotkeycuk"];
    
    NSDictionary *paramer = @{@"sign":str};
    
    NSString *urls = [NSString stringWithFormat:@"http://api.dw521.com/api/login/login.php?action=reset"];
    [manager POST:urls parameters:paramer success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"登录信息responseObject=%@",responseObject);
        EncryptionData *encryption = [[EncryptionData alloc]init];
        NSString * str =[encryption decryption:[responseObject objectForKey:@"sign"] :@"hotkeycuk"];
        NSDictionary *dic = [str objectFromJSONString];
        [self.delegate updataPassword:dic];
    
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error");
        [self.delegate requistFail];
    }];
}
#pragma mark - 微信验证
-(void)checkwx:(NSString *)OpenID{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    //NSMutableArray *array = [[NSMutableArray alloc]init];
    NSMutableDictionary *  smallGoodsDic = [NSMutableDictionary dictionaryWithCapacity:1];
    
    [smallGoodsDic setObject:OpenID forKey:@"OpenID"];
    //[array addObject:smallGoodsDic];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:smallGoodsDic options:NSJSONWritingPrettyPrinted error:nil];
    
    NSString *jsonString = [jsonData base64EncodedStringWithOptions:0];
    
    // EncryptionData *data  = [[EncryptionData alloc]init];
    NSString *str = [self encodeString:jsonString :@"hotkeycuk"];
    
    NSDictionary *paramer = @{@"sign":str};
    
    NSString *urls = [NSString stringWithFormat:@"http://api.dw521.com/api/login/login.php?action=checkwx"];
    [manager POST:urls parameters:paramer success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"登录信息responseObject=%@",responseObject);
        EncryptionData *encryption = [[EncryptionData alloc]init];
        NSString * str =[encryption decryption:[responseObject objectForKey:@"sign"] :@"hotkeycuk"];
        NSDictionary *dic = [str objectFromJSONString];
        [self.delegate checkwx:dic];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error = %@",error);
        [self.delegate requistFail];
    }];

}

#pragma mark - 微信登陆
-(void)bindwx:(NSString *)OpenID andMobileNumber:(NSString *)MobileNumber andLoginPass:(NSString *)LoginPass{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    //NSMutableArray *array = [[NSMutableArray alloc]init];
    NSMutableDictionary *  smallGoodsDic = [NSMutableDictionary dictionaryWithCapacity:1];
    
    [smallGoodsDic setObject:OpenID forKey:@"OpenID"];
    [smallGoodsDic setObject:MobileNumber forKey:@"MobileNumber"];
    [smallGoodsDic setObject:LoginPass forKey:@"LoginPass"];

    //[array addObject:smallGoodsDic];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:smallGoodsDic options:NSJSONWritingPrettyPrinted error:nil];
    
    NSString *jsonString = [jsonData base64EncodedStringWithOptions:0];
    
    // EncryptionData *data  = [[EncryptionData alloc]init];
    NSString *str = [self encodeString:jsonString :@"hotkeycuk"];
    
    NSDictionary *paramer = @{@"sign":str};
    
    NSString *urls = [NSString stringWithFormat:@"http://api.dw521.com/api/login/login.php?action=bindwx"];
    [manager POST:urls parameters:paramer success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"登录信息responseObject=%@",responseObject);
        EncryptionData *encryption = [[EncryptionData alloc]init];
        NSString * str =[encryption decryption:[responseObject objectForKey:@"sign"] :@"hotkeycuk"];
        NSDictionary *dic = [str objectFromJSONString];
        [self.delegate bindwx:dic];
  
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.delegate requistFail];
    }];
}
#pragma mark - 版本检测
-(void)version:(NSString *)Version andDeviceType:(NSString *)DeviceType{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    //NSMutableArray *array = [[NSMutableArray alloc]init];
    NSMutableDictionary *  smallGoodsDic = [NSMutableDictionary dictionaryWithCapacity:1];
    
    [smallGoodsDic setObject:Version forKey:@"Version"];
    [smallGoodsDic setObject:DeviceType forKey:@"DeviceType"];
    
    //[array addObject:smallGoodsDic];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:smallGoodsDic options:NSJSONWritingPrettyPrinted error:nil];
    
    NSString *jsonString = [jsonData base64EncodedStringWithOptions:0];
    
    // EncryptionData *data  = [[EncryptionData alloc]init];
    NSString *str = [self encodeString:jsonString :@"hotkeycuk"];
    
    NSDictionary *paramer = @{@"sign":str};
    
    NSString *urls = [NSString stringWithFormat:@"http://api.dw521.com/api/login/version.php?action=check"];
    [manager POST:urls parameters:paramer success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"登录信息responseObject=%@",responseObject);
        EncryptionData *encryption = [[EncryptionData alloc]init];
        NSString * str =[encryption decryption:[responseObject objectForKey:@"sign"] :@"hotkeycuk"];
        NSDictionary *dic = [str objectFromJSONString];
        [self.delegate version:dic];
   
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
        [self.delegate requistFail];
    }];

}
#pragma mark - 加密
-(NSString *)encodeString:(NSString *)data :(NSString *)key{
    //    NSData* sampleData = [data dataUsingEncoding:NSUTF8StringEncoding];
    //
    //    NSString * base64String = [sampleData base64EncodedStringWithOptions:0];
    NSLog(@"%@",data);
    NSString *result=[[NSMutableString alloc] init];
    NSInteger keyLength = [key length];
    int position = 0;
    int keyChar = 0;
    int strChar = 0;
    for (int i = 0; i < [data length]; i++) {
        position = i % keyLength;
        strChar = [data characterAtIndex:i];
        keyChar =[key characterAtIndex:position];
        int  ch = strChar ^ keyChar;
        char ca = (char) ch;
        result=[result stringByAppendingString:[NSString stringWithFormat:@"%C",ca]];
    }
    
    NSData* sampleDat1a = [result dataUsingEncoding:NSUTF8StringEncoding];
    
    NSString * base64String1 = [sampleDat1a base64EncodedStringWithOptions:0];
    
    return base64String1;
}
@end
